from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView,Response
from myapp.models import User,Good,Ding
from django.contrib.auth.hashers import check_password,make_password
from utils.my_celery.send_code_tasks import send_code
import re,time,json,redis
#短信验证码
class SMScodeView(APIView):
    def get(self,request):
        mobile=request.query_params.get('mobile')
        if not re.findall('^1[3-9]\d{9}$',mobile):
            return Response({'code':205,'msg':'手机号不正确'})
        # resp=send_message(mobile)   #。。。。。。。。。

        resp=send_code.delay(mobile)
        time.sleep(3)
        try:
           cls=redis.Redis(host='127.0.0.1',port=6379,db=5)
           key='celery-task-meta-'+resp.id
           redis_data=cls.get(key)

           if redis_data:
               distct_resp=json.loads(redis_data.decode())
               if  distct_resp['status']=='SUCCESS':
                  dict_data=json.loads(distct_resp['result'])
                  if  dict_data['statusCode']=='112310' or dict_data['statusCode']=='000000':
                     return  Response({'msg':'验证码发送成功','code':200})
               return Response({'code':203,'msg':'错误'})
           else:
                return  Response({'msg':'验证码发送失败','code':206})
        except  Exception as  e:
            print('错误原因',e)
            return Response({'code':500,'msg':'后端错误'})





class Login(APIView):
    def post(self,request):
            mobile = request.data.get('mobile')
            password = request.data.get('password')
            try:
                user = User.objects.get(mobile=mobile)
                if password!=user.password:
                    return Response({'code':400,'msg':'密码不对'})
                return Response({'code':200,'msg':'登录成功','yhm':user.id})
            except Exception as e:
                print(e)
                user = User.objects.create(mobile=mobile,password=password)
                return Response({'code': 200, 'msg': '注册成功','yhm':user.id})

class GoodView(APIView):
    def get(self,request):
        good = Good.objects.all()
        list = []
        for i in good:
            list.append({
                'id':i.id,
                'img':i.img,
                'title':i.title,
                'price':i.price,
            })
        return Response({'code':200,'msg':'加载成功','list':list})
from .serializer import GoodSer,DingSer
class AGood(APIView):
    def get(self,request,id):
        good = Good.objects.get(id=id)
        ser = GoodSer(good)
        return ser.data

class DingView(APIView):
    def post(self,request):
        name = request.data.get('name')
        price = request.data.get('price')
        img = request.data.get('img')
        ss = request.data.get('ss')
        try:
            Ding.objects.create(name=name,price=price,img=img,s_id=ss)
            return Response({'code':200,'msg':'创建成功'})
        except Exception as e:
            print(e)
            return Response({'code':400,'msg':'创建失败'})


















